package com.azt.front.action.member;

import com.azt.api.pojo.*;
import com.azt.api.service.MemberService;
import com.azt.api.service.ProductService;
import com.azt.front.permission.RequireCompany;
import com.azt.front.utils.ContextUtil;
import com.azt.utils.MathUtil;
import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 
          /~~~~\   /~~~~\
          \    /   \    /
           |  |_____|  |
         /~             ~\
/~~~~~-_|  /~~\    /~~~\  |_-~~~~~\
\ ==== /| |   O|  |   ^ | |\ ==== /
 ~-__-~ | |_---+--+----_| | ~-__-~
        |/~             ~\|
        /     sayolala    \
       (      O      O     )
        `\              ./'
          ~-__________-~|
          |   |\__/|    |
          |   |/~~\|    |
          | /~~~~~~~~~\ |
          | |         | |
          | |         | |
          | |         | |
          | |         | |
 */
@Controller
@RequestMapping("/cenMember")
@RequireCompany
public class CenMemberAction {
    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductService productService;

    /**
     * 集采统计
     * */
    @RequestMapping("/souce")
    public String souceState(Model model) {
        Company company = ContextUtil.getCompany();

        Member member = memberService.getCenMember(company.getId());
        double enqPrice = 0;
        double mallPrice = 0;
        double notPayPrice = 0;
        int stage = 0;
        double payPrice = 0;
        if(member != null) {
            //报价成交金额
            enqPrice = memberService.getTotalPayPrice(2, company.getId(), member.getSignintime(), DateUtil.offsetDay(member.getEndtime(), 1));
            //商城成交金额
            mallPrice = memberService.getTotalPayPrice(1, company.getId(), member.getSignintime(), DateUtil.offsetDay(member.getEndtime(), 1));
            //已完成阶段

            //已交返点金额
            //还需交返点金额

            CenCollection coll1 = memberService.selectEnqCollection(0, member.getId());//待收款对象信息
            CenCollection coll2 = memberService.selectEnqCollection(1, member.getId());//已收款对象信息
            notPayPrice = coll1 == null ? 0 : coll1.getMon();
            payPrice = coll2 == null ? 0 : coll2.getMon();
            stage = memberService.getReachedStage(member.getId());
        }
        //当前销售总额
        double totalPrice = MathUtil.add(enqPrice, mallPrice);
        model.addAttribute("totalPrice", totalPrice);
        model.addAttribute("enqPrice", enqPrice);
        model.addAttribute("mallPrice", mallPrice);
        model.addAttribute("notPayPrice", notPayPrice);
        model.addAttribute("stage", stage);
        model.addAttribute("payPrice", payPrice);
        return "member/souceView";
    }

    @RequestMapping("/my")
    public String mysign(Model model) {
        Company company = ContextUtil.getCompany();

        Member mem = memberService.getCenMember(company.getId());

        if (mem != null) {
            List<MemberDetail> memberDetailList = memberService.findMyMemberDetail(mem.getId());

            Double enqPrice = memberService.getTotalPayPrice(2, company.getId(), mem.getSignintime(), DateUtil.offsetDay(mem.getEndtime(), 1));
            Double mallPrice = memberService.getTotalPayPrice(1, company.getId(), mem.getSignintime(), DateUtil.offsetDay(mem.getEndtime(), 1));
            double jytotal = 0;
            if (enqPrice != null) {
                jytotal += enqPrice;
            }
            if (mallPrice != null) {
                jytotal += mallPrice;
            }

            if (memberDetailList.size() > 0) {//设置长读条样式的width 百分比
                for (int i = 0; i < memberDetailList.size(); i++) {

                    double result = MathUtil.divide(jytotal, memberDetailList.get(i).getMaxNum(), 2);
                    if (result >= 1) {
                        memberDetailList.get(i).setPrecent(1.0);
                        memberDetailList.get(i).setIsFull(1);
                    } else {
                        memberDetailList.get(i).setPrecent(result);
                        memberDetailList.get(i).setIsFull(0);
                    }

                }
            }

            List<Product> productList = productService.findProductWithCenSku(company.getId());
            model.addAttribute("productList", productList);//集采商品
            model.addAttribute("details", memberDetailList);
            model.addAttribute("jytotal", jytotal);
            model.addAttribute("member", mem);//集采商家
        }
        return "member/mysign";

    }
}
